#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
using namespace std;

void handler(int signo)
{
    cout << "handler " << signo << endl;
} 

void PrintPending(const sigset_t& pending)
{
    for(int signo = 31; signo > 0; signo--)
    {
        if(sigismember(&pending, signo))
        {
            cout << "1";
        }      
        else
        {
            cout << "0";
        }
    }
    cout << endl;
}

int main()
{
    signal(2, handler);
    cout << "getpid: " << getpid() << endl;
    //1.屏蔽2号信号
    sigset_t set, oset;
    sigemptyset(&set);
    sigemptyset(&oset);
    sigaddset(&set, 2); 
    sigprocmask(SIG_BLOCK, &set, &oset);
    //2.让进程不断获取当前进程的pending表
    int cnt = 0;
    sigset_t pending;
    while(true)
    {
        sigpending(&pending);
        PrintPending(pending);     
        sleep(1);
        cnt++;
        if(cnt == 10)
        {
            cout << "解除对2号信号的屏蔽, 2号信号准备递达" << endl;
            sigprocmask(SIG_SETMASK, &oset, nullptr);
        }
    }
    return 0;
}